import React, {PureComponent} from "react";
import {notification, Table} from "antd";
import {connect} from "react-redux";
import {paginationProps, tableProps} from "../../lib/ui";
import {query, showModify} from "./actions";
import {organizations} from "../home/actions";
import {getLastFilter, getPrincipal} from "../../lib/identity";
const LAST_FILTER = "historyNotice";

class List extends PureComponent {

    onPageChange = (page, pageSize) => {
        const {dispatch} = this.props;
        dispatch(query({page, pageSize}));
    };


    componentWillMount() {
        const {dispatch} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        dispatch(query({...fields}));
        dispatch(organizations(getPrincipal().id));
    }

    handleShowEdit = row => {
        const {dispatch} = this.props;
        dispatch(showModify({...row}));
    };

    render() {
        const columns = [
            {
                title: "标题",
                dataIndex: "title",
                width: "180px",
                render: (text, record) => (
                    <a
                        onClick={() => {
                            this.handleShowEdit(record);
                        }}
                    >
                        {text}
                    </a>
                )
            }, {
                title: "有效时间",
                dataIndex: "time",
                width: "200px",
                render: (text, record) => (
                    <span>{record.startTime.substring(0, 10)} 至 {record.endTime.substring(0, 10)}</span>
                )
            }, {
                title: "状态",
                dataIndex: "status",
                width: "100px"
            }, {
                title: "可见范围",
                dataIndex: "organizationNames",
                width: "400px"
            }, {
                title: "内容",
                dataIndex: "description",
                ellipsis: true
            }
        ];
        const {
            page,
            pageSize,
            totalElements,
            dataSource,
            loading
        } = this.props;

        const tablePagination = {
            ...paginationProps,
            total: totalElements,
            current: page,
            pageSize: pageSize,
            onShowSizeChange: (current, newSize) =>
                this.onPageChange && this.onPageChange(1, newSize),
            onChange: this.onPageChange
        };
        return (
            <Table
                {...tableProps}
                columns={columns}
                pagination={tablePagination}
                scroll={{x: 1500, y: 'calc(100vh - 350px)'}}
                dataSource={dataSource}
                loading={loading}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.historyNotice.list
    };
};

export default connect(mapStateToProps)(List);
